﻿
#region Imports...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Diagnostics;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pharmacy.Data;
using Pharmacy.Entities;
using Pharmacy.Settings.BusinessLayer;
using Pharmacy.Web.UI;
#endregion

public partial class PoEdit : System.Web.UI.Page
{
    public string HasGRN = "inherit";
    protected void Page_Load(object sender, EventArgs e)
    {
        FormUtil.RedirectAfterInsertUpdate(FormView1, "PoEdit.aspx?{0}", PoDataSource);
        FormUtil.RedirectAfterAddNew(FormView1, "PoEdit.aspx");
        FormUtil.RedirectAfterCancel(FormView1, "Po.aspx");
        FormUtil.SetDefaultMode(FormView1, "PoId");
        if (!IsPostBack)
        {
            dataDrugId.DataBind();
            dataDrugId.Items.Insert(0, new ListItem("<...Please Choose...>", ""));
        }
    }
    protected void GridViewGrn1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string urlParams = string.Format("GrnId={0}", GridViewGrn1.SelectedDataKey.Values[0]);
        Response.Redirect("GrnEdit.aspx?" + urlParams, true);
    }
    protected void GridViewPoDetails2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string urlParams = string.Format("PoDetailId={0}", GridViewPoDetails2.SelectedDataKey.Values[0]);
        Response.Redirect("PoDetailsEdit.aspx?" + urlParams, true);
    }
    protected void FormView1_Load(object sender, EventArgs e)
    {
        DropDownList dataRequestBy = (DropDownList)FormView1.Row.FindControl("dataRequestBy");
        dataRequestBy.Enabled = false;
        if (FormView1.CurrentMode == FormViewMode.Insert)
        {
            dataRequestBy.SelectedValue = Request.Cookies["LoginUser"].Value;
            TextBox dataPOID = (TextBox)FormView1.Row.FindControl("dataPOID");
            TextBox dataPoDate = (TextBox)FormView1.Row.FindControl("dataPoDate");
            string Perfix = ServiceFacade.SettingsHelper.POPrefix + DateTime.Now.ToString("yyMMdd");
            int Count = 0;
            TList<Po> objPo = DataRepository.PoProvider.GetPaged("PoId like '" + Perfix + "' + '%'", "PoId desc", 0, 1, out Count);
            if (Count == 0)
                dataPOID.Text = Perfix + "001";
            else
            {
                dataPOID.Text = Perfix + String.Format("{0:000}", int.Parse(objPo[0].PoId.Substring(objPo[0].PoId.Length - 3)) + 1);
            }
            dataPoDate.Text = DateTime.Now.ToString("dd-MMM-yyyy");
            dataPoDate.Enabled = false;
          
        }
        else
        {
            FormView1.FooterRow.FindControl("PreviewButton").Visible = true;

            string poId = Request.QueryString["PoId"];
            Po objPo = DataRepository.PoProvider.GetByPoId(poId);

            int count = 0;
            DataRepository.GrnProvider.GetTotalItems("PoId='" + poId + "'", out count);
            if (count > 0)
                HasGRN = "none";
            if (HasGRN == "none")
                FormView1.FooterRow.FindControl("UpdateButton").Visible = false;
            else
                pn_PODetailsAdd.Visible = true;

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.Text == "Add")
        {
            TextBox dataPOID = (TextBox)FormView1.Row.FindControl("dataPOID");
            string whereExpression = string.Format("PoId='{0}' And DrugId='{1}'", dataPOID.Text,
                                                   dataDrugId.SelectedValue);
            int count = 0;
            PoDetails objDetails;
            TList<PoDetails> list = DataRepository.PoDetailsProvider.GetPaged(whereExpression, "", 0, 1, out count);
            if (count > 0)
                objDetails = list[0];
            else
                objDetails = new PoDetails();
            objDetails.DrugId = dataDrugId.SelectedValue;
            objDetails.Quantity = Int32.Parse(tbQuantity.Value);
            objDetails.Remark = tbRemark.Value;
            objDetails.RemainQuantity = objDetails.Quantity;
            objDetails.PoId = dataPOID.Text;
            DataRepository.PoDetailsProvider.Save(objDetails);
            GridViewPoDetails2.DataBind();
            tbRemark.Value = "";
            tbQuantity.Value = "1";
            dataDrugId.SelectedValue = "";
            dataDrugId.Focus();
        }
    }
    [WebMethod]
    public static void DeletePoDetail(string poDetailId)
    {
        DataRepository.PoDetailsProvider.Delete(long.Parse(poDetailId));
    }
    protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        if (e.CommandName == "Preview")
        {
            string poId = Request.QueryString["PoId"];
            Response.Redirect("PreviewPO.aspx?PoId=" + poId);
        }
    }
}


